<?php
/**
 * Created by Sublime.
 * Author: lingyun
 * Date: 2017-12-05 15:49:21
 * @desc 
 */
namespace app\mall\controller;
use controller\BasicAdmin;
use service\DataService;
use think\Db;
use think\Request;
use app\mall\model\GoodsStockLog;
class Goodsstock extends BasicAdmin{
	/**
	 * [index description]
	 * @Author   lingyun
	 * @DateTime 2017-12-11T15:30:38+0800
	 * @return   [type]                   [description]
	 */
	public function index(){
		$id = input('id');		//商品id
		$list = Db::name('goods_spec_detail')->where(['goods_id'=>$id,'is_deleted'=>0])->select();		//商品属性详情
		$goods = Db::name('goods')->where('id',$id)->find();		//商品详情
		$this->assign('list',$list);
		$this->assign('goods',$goods);
		$this->assign('title','商品库存');
		return $this->fetch('index');
	}

	/**
	 * 根据商品属性变更商品库存
	 * @Author   lingyun
	 * @DateTime 2017-12-05T15:55:20+0800
	 * @return   [type]
	 */
	public function change_stock(){
		if(request()->isPost()){
			$post = input('post.');
			// if(!empty($goods['goods_spec_id'])){
			// 	$stock = Db::name('goods_spec_detail')->where(['goods_id'=>$post['goods_id'],'id'=>$post['goods_spec_id']])->value('stock');
			// }else{
			// 	$stock = Db::name('goods_spec_detail')->where(['goods_id'=>$post['goods_id']])->value('stock');
			// }
			$stock = Db::name('goods_spec_detail')->where(['goods_id'=>$post['goods_id'],'id'=>$post['goods_spec_id']])->value('stock');			//商品库存    
			// halt($stock);
			unset($post['goods_name']);
			// $model = new GoodsStockLog();
			if($post['sign'] == 1){
				$post['cur_num'] = $post['rec_num']+$stock;		//当前库存
			}else{
				$post['cur_num'] = $stock-$post['dif_num'];		//当前库存
			}
			$post['addtime'] = time();
			if($post['sign'] == 1){
				$post['dif_num'] = $post['rec_num'] - $post['del_num'];			//差异数
			}
			empty($post['goods_spec_id'])?$post['goods_spec_id']=0:$post['goods_spec_id'];
			$post['sign']==1?$post['type']=1:$post['type']=2;		//1增加，2减少
			$res = Db::name('goods_stock_log')->insertGetId($post);
			if($res > 0){
				if($post['sign'] == 1){
					$res = Db::name('goods_spec_detail')->where('id',$post['goods_spec_id'])->setInc('stock',$post['rec_num']);	
				}else{
					$res = Db::name('goods_spec_detail')->where('id',$post['goods_spec_id'])->setDec('stock',$post['dif_num']);	
				}
			}else{
				$this->error('保存失败','');
			}
			$res!==false?$this->success('保存成功',''):$this->error('保存失败','');
		}else{
			$goods_id = input('goods_id');		//商品ID
			$goods_spec_id = input('goods_spec_id');		//商品属性id
			$goods = Db::name('goods')->where('id',$goods_id)->find();		//商品名称
			$goods_spec = Db::name('goods_spec_detail')->where(['id'=>$goods_spec_id,'is_deleted'=>0])->find();	//商品属性
			$this->assign('goods_spec',$goods_spec);
			$this->assign('goods_id',$goods_id);		//商品ID
			$this->assign('goods_spec_id',$goods_spec_id);
			$this->assign('title','变更库存');
			$this->assign('goods',$goods);
			return $this->fetch('change_stock');
		}
	}
	/**
	 * 库存日志
	 * @Author   lingyun
	 * @DateTime 2017-12-11T21:01:59+0800
	 * @return   [type]                   [description]
	 */
	public function stock_log(){
		$this->title = "库存日志";
		$post = input('get.');
		$get = $this->request->get();
		$join = [
			['ys_store b','a.store_id=b.id','LEFT'],
			['ys_goods_spec_detail c','a.goods_spec_id=c.id','LEFT'],
			['ys_goods d','a.goods_id=d.id','LEFT']
		];
		$db = Db::name('goods_stock_log')->alias('a')->join($join)->where(['a.goods_id'=>$post['goods_id'],'a.goods_spec_id'=>$post['goods_spec_id']])->field('a.*,b.title,c.title as spec,d.name');
		if(isset($get['date']) && $get['date'] !== '') {
            list($start, $end) = explode(' - ', $get['date']);
            $start = $start."  00:00:00";		$end = $end." 23:59:59";
            $start = strtotime($start);			$end = strtotime($end);
            $db->whereBetween('a.addtime',["{$start}", "{$end}"]);
        }
		return parent::_list($db);
	}


}